package com.terry.test3;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 计算矩形面积
 *
 * @author 小八
 * @date 2023年02月14日13:42
 */
class RectP{
    public int w;
    public int h;
    public int x;
    public int y;
    public RectP(int x,int y,int w,int h){
        this.w = w ;
        this.h = h ;
        this.x = x ;
        this.y = y ;
    }
}
public class Title41 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
//        RectP rect1 = new RectP(0,2,2,2);
//        RectP rect2 = new RectP(-1,1,2,2);


            List<RectP> list = new ArrayList<>();
            List<RectP> newList = new ArrayList<>();

            Deque<RectP> deque = new ArrayDeque<>();
        while(in.hasNextLine()) {
            String[] split = in.nextLine().split(" ");
            RectP curRect = new RectP(Integer.parseInt(split[1]),
                Integer.parseInt(split[2]),
                Integer.parseInt(split[3]),
                Integer.parseInt(split[4]));
            //绘制
            if(split[0].equals("d")){
                if(list.isEmpty()){
                    list.add(curRect);
                }else{
                    deque.addLast(curRect);
                    while(!deque.isEmpty()) {
                        RectP calcRect = deque.removeFirst();
                        Iterator<RectP> iterator = list.iterator();
                        boolean nofind = false;
                        while (iterator.hasNext()) {
                            RectP temp = iterator.next();
                            //获取相交面积
                            RectP p = getIntersect(temp, calcRect);
                            //没找到继续找
                            if (p == null) {
                                nofind = true;
                                continue;
                            } else if (p == calcRect) {//内涵
                                nofind = false;
                                iterator.remove();
                                newList.add(calcRect);
                                break;
                            }else if (p == temp) {//内涵
                                nofind = false;
                                break;
                            }else { //相交
                                nofind = false;
                                newList.addAll(splitRect(temp, p));
                                iterator.remove();
                                //剩余的
                                deque.addAll(splitRect(calcRect, p));
                                break;
                            }
                        }
                        if(nofind){
                            list.add(calcRect);
                        }
                        list.addAll(newList);
                        newList.clear();
                    }
                    System.out.println(list.stream().map(obj -> obj.h * obj.w).collect(Collectors.summarizingInt(Integer::intValue)).getSum());
                }
            }else {//擦除
                deque.addLast(curRect);
                while(!deque.isEmpty()) {
                    RectP calcRect = deque.removeFirst();
                    Iterator<RectP> iterator = list.iterator();
                    boolean nofind = false;
                    while (iterator.hasNext()) {
                        RectP temp = iterator.next();
                        //获取相交面积
                        RectP p = getIntersect(temp, calcRect);
                        //没找到继续找
                        if (p == null) {
                            nofind = true;
                            continue;
                        } else if (p == calcRect) {//内涵
                            nofind = false;
                            iterator.remove();
                            deque.addAll(splitRect(calcRect, temp));
                            break;
                        }else if (p == temp) {//内涵
                            nofind = false;
                            iterator.remove();
                            deque.addAll(splitRect(temp, calcRect));
                            break;
                        }else { //相交
                            nofind = false;
                            newList.addAll(splitRect(temp, p));
                            iterator.remove();
                            //剩余的
                            deque.addAll(splitRect(curRect, p));
                            break;
                        }
                    }
                    if(nofind){
                        list.add(calcRect);
                    }
                    list.addAll(newList);
                    newList.clear();
                }
                System.out.println(list.stream().map(obj -> obj.h * obj.w).collect(Collectors.summarizingInt(Integer::intValue)).getSum());
            }
        }
    }
    //计算剩余的矩形
    public static List<RectP> splitRect( RectP p1,RectP p2){
        List<RectP> rects = new ArrayList<>();
        rects.add(new RectP(p1.x,p1.y,p2.x-p1.x,p1.y-p2.y));//左上
        rects.add(new RectP(p2.x,p1.y,p2.w,p1.y-p2.y));//中上
        rects.add(new RectP(p2.x+p2.w,p1.y,p1.w-(p2.w-p2.x-p1.x),p1.y-p2.y)); //右上
        rects.add(new RectP(p2.x,p2.y,p2.x-p1.x,p2.h)); //左中
        rects.add(new RectP(p2.x+p2.w,p2.y,p1.w-(p2.w-p2.x-p1.x),p2.h)); //右中
        rects.add(new RectP(p1.x,p2.y+p2.h,p2.x-p1.x,p1.h-p2.h-(p1.y-p2.y)));//左下
        rects.add(new RectP(p2.x,p2.y+p2.h,p2.w,p1.h-p2.h-(p1.y-p2.y)));//中下
        rects.add(new RectP(p2.x+p2.w,p2.y+p2.h,p1.w-(p2.w-p2.x-p1.x),p1.h-p2.h-(p1.y-p2.y))); //右下

        return rects.stream().filter(obj-> obj.w !=0 && obj.h !=0).collect(Collectors.toList());
    }

    public static RectP getIntersect(RectP r1,RectP r2){
        //不相交
        if(r2.x >= r1.x+r1.w  || r2.x + r2.w<= r1.x || r2.y<=r1.y-r1.h || r2.y-r2.h >= r1.y){
            return null;
        }else if(r2.x <= r1.x && r2.y <= r1.y){ // 左下角相交
            return new RectP(r1.x,r2.y,r2.w-(r1.x-r2.x),r1.h-(r1.y-r2.y));
        }else if(r2.x>=r1.x && r2.y <= r1.y){//右下角相交
            return new RectP(r2.x,r2.y,r1.w-(r2.x-r1.x),r1.h-(r1.y-r2.y));
        }else if(r2.x <= r1.x && r2.y >= r1.y){ //左上角相交
            return new RectP(r1.x,r1.y,r2.w-(r1.x-r2.x),r2.h-(r2.y-r1.y));
        }else if(r2.x >= r1.x && r2.y >= r1.y){ //右上角相交
            return new RectP(r2.x,r1.y,r1.w-(r2.x-r1.x),r2.h-(r2.y-r1.y));
        }else{
            System.out.println("出现内涵");
            if(r1.w > r2.w){
                return r1;
            }
            return r2;
        }
    }
}
